package top.tagao.m03_beHavioralPatterns.b_13chain.server;

import top.tagao.m03_beHavioralPatterns.b_13chain.middleware.MiddleWare;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Viktor.Liu
 * @Date 2023/4/28 17:38
 */
public class Server {
    private Map<String, String> users = new HashMap<>();
    private MiddleWare middleware;

    /**
     * Client passes a chain of object to server. This improves flexibility and
     * makes testing the server class easier.
     */
    public void setMiddleware(MiddleWare middleware) {
        this.middleware = middleware;
    }



    public void register(String email, String password) {
        users.put(email, password);
    }
    public boolean hasEmail(String email) {
        return users.containsKey(email);
    }

    public boolean isValidPassword(String email, String password) {
        return users.get(email).equals(password);
    }

    public void logIn(String email, String password) {
        if (!middleware.handle(email, password))     System.out.println("Authorization have been successful!");

    }
}
